from sqlalchemy.orm import DeclarativeBase, mapped_column
from sqlalchemy import String, text
from typing import Annotated
from datetime import datetime
from enum import Enum


# Own type declaration
str_256 = Annotated[str, 256]

# Type aliases
created_at = Annotated[
    datetime, 
    mapped_column(server_default=text("TIMEZONE('utc', now())")),
]
updated_at = Annotated[
    datetime, 
    mapped_column(
        server_default=text("TIMEZONE('utc', now())"),
        onupdate=datetime.utcnow,
    ),
]

# Enum type
Sex = Enum(
    value= "Sex",
    names = ["Женщина", "Мужчина"],
)


# Base class for all models that stores metadata of all tables
class Base(DeclarativeBase):
    # Annotation map of own types to Mapping types
    type_annotation_map = {
        str_256: String(256),
    }
    
    
    # For printing representation of model (without relations)
    def __repr__(self):
        cols = []
        for col in self.__table__.columns.keys():
            cols.append(f"{col}={getattr(self, col)}")
        return f"<{self.__class__.__name__}: {', '.join(cols)}>"